import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import User.Rating;
import User.User;

/**
 * 
 * @author Tom
 *
 *	Update triangle and euclidean when a user has rated a new item AND expand when a new item is added
 *
 */
public class Datamining
{
	public Datamining()
	{		
		long start = System.currentTimeMillis();
		try
		{
			readFile("dataset4.txt");
		}
		catch (IOException e) {}
		System.out.println(System.currentTimeMillis() - start);
		
//		Utils.printEuclidean();
//		System.out.println();
		
		int userId;
		Scanner sc = new Scanner(System.in);
		while (true)
		{
			System.out.println("Enter the user id to get an advice or \"quit\" to stop.");
			
			String next = sc.next();
			
			if (next.equals("quit"))
			{
				break;
			}
			
			try
			{
				userId = Integer.parseInt(next);
				User u = Utils.getUser(userId);
				
				if (u.getRatings().length < 2)
				{
					throw new NullPointerException();
				}
				
				Rating[] advice = Utils.getAdvice(u);

				if (advice != null)
				{
					for (int i = 0; i < Math.min(advice.length, 5); i++)
					{
						System.out.println(advice[i]);
					}
					
					System.out.println("---------------------------");
				}
				
				Rating[] rs = Utils.getSlopeAdvice(u);
				for (int i = 0; i < Math.min(rs.length, 5); i++)
				{
					System.out.println(rs[i]);
				}
				
				System.out.println();
			}
			catch (NumberFormatException | NullPointerException e)
			{
				e.printStackTrace();
				System.out.println("Could not create a recommendation!");
			}
		}
		
		sc.close();
	}
	
	public static void main(String[] args)
	{
		new Datamining();
	}

	public void readFile(String file) throws IOException
	{
		BufferedReader br = new BufferedReader(new FileReader(file));
		try
		{
			String line = br.readLine();

			while (line != null)
			{
				String[] parts = line.split("\t");
//				String[] parts = line.split(",");
				Utils.addRating(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Double.parseDouble(parts[2]));

				line = br.readLine();
			}
		}
		finally
		{
			br.close();
		}
	}
}
